package com.mmal.example.syncContainer;

import com.mmal.annotation.NotThreadSafe;

import java.util.Vector;

/**
 * Created by sigurHe
 * Date:2019/10/7.
 * Time:18:32.
 */
@NotThreadSafe
public class VectorExample2 {


    private static Vector<Integer> vector = new Vector<>();

    public static void main(String[] args) {

        while (true) {
            for (int i = 0; i < 10; i++) {
                vector.add(i);
            }

            // 因为操作顺序的差异，下面get方法要取index为9的数据
            // 上面的线程刚好执行到这一句，把9的元素移除掉了，这样就会引起角标越界异常

            Thread thread1 = new Thread(() -> {
                for (int i = 0; i < vector.size(); i++) {
                    vector.remove(i);
                }
            });

            Thread thread2 = new Thread(() -> {
                for (int i = 0; i < vector.size(); i++) {
                    vector.get(i);
                }
            });
            thread1.start();
            thread2.start();
        }
    }
}
